/* GaussSolver����A,B������,vector��Ϊ����
 * 
 */
package com.simulation;

import java.util.ArrayList;

class TMathPro
{
    static double Interpolate(ArrayList<Double> x,ArrayList<Double> y, double w) {
    	double v =1.0e+8;
    	for (int j=0;j<x.size()-1;++j)
    	{
    		if ((w >= x.get(j))&&(w <= x.get(j+1)))
    		{
    			v = (w-x.get(j+1))*y.get(j)/(x.get(j)-x.get(j+1))+(w-x.get(j))*y.get(j+1)/(x.get(j+1)-x.get(j));
    			return v;
    		}
    	}
    	if ((w < x.get(0))||(w > x.get(x.size()-1)))
    	{
    		v = 1.0e+8;
    	}
    	return v;
    }
    
	static double I_Interpolate(double x[],double y[], double w) {
		double v= 1.0e+8;
		for (int j=0;j<16-1;++j)
		{
			if ((w >= x[j])&&(w <= x[j+1]))
			{
				v = (w-x[j+1])*y[j]/(x[j]-x[j+1])+(w-x[j])*y[j+1]/(x[j+1]-x[j]);
				return v;
			}
		}
		if ((w < x[0])||(w > x[15]))
		{
			v = 1.0e+8;
		}
		return v;
	};
    static double Max(double a, double b) {
    	if(a>b)
        {
            return a;
        }
        else
        {
            return b;
        }
    }
    
    static void ChaseSolver(double A[],double B[],
                           double C[],double D[],
                           double X[])
    {
    	int NR = All.NR;
    	double R[] = new double[NR];
    	double Y[] = new double[NR];
    	double Q;

    	R[0]=C[0]/B[0];
    	Y[0]=D[0]/B[0];
    	for(int i=1;i<NR-1;i++)
    	{
    		Q=B[i]-R[i-1]*A[i];
    		R[i]=C[i]/Q;
    		Y[i]=(D[i]-Y[i-1]*A[i])/Q;
    	}

    	Y[NR-1]=(D[NR-1]-Y[NR-2]*A[NR-1])/(B[NR-1]-R[NR-2]*A[NR-1]);
    	X[NR-1]=Y[NR-1];

    	for(int i=NR-2;i>=0;i--)
    	{
    		X[i]=Y[i]-R[i]*X[i+1];
    	}
    }
    
    static void GaussSolver(double[][] A, double[] B) {
    	final double dd = 0.00000001;
    	double maxValue;
    	int index;
    	double tempt;
    	double con,sum;

    	for(int j=0;j<A[0].length-1;j++)
    	{
    		maxValue=Math.abs(A[j][j]);
    		index=j;
    		for(int i=j+1;i<A.length;i++)
    		{
    			if(Math.abs(A[i][j])>maxValue)
    			{
    				maxValue= Math.abs(A[i][j]);
    				index=i;
    			}
    		}

    		if(index>j)
    		{
    			for(int i=0;i<A[j].length;i++)
    			{
    				tempt= A[j][i];
    				A[j][i]=A[index][i];
    				A[index][i]=tempt;
    			}
    			tempt=B[j];
    			B[j]=B[index];
    			B[index]=tempt;
    		}

    		if(Math.abs(A[j][j]-0)<dd)
    		{
    			//Application->MessageBoxA("the equation main coefficient be 0 !","Information",MB_OK);
    			return;

    		}
    		else
    		{
    			for(int i=j+1;i<A.length;i++)
    			{
    				if(Math.abs(A[i][j]-0)>dd)
    				{
    					con=A[i][j]/A[j][j];
    					for(int k=0; k<A[j].length;k++)
    					{
    						A[i][k]=A[i][k]-A[j][k]*con;
    					}
    					B[i]=B[i]-B[j]*con;
    				}//end if
    			}  //end for i
    		}// end else
    	} //end for j

    	B[B.length-1]=B[B.length-1]/A[A.length-1][A.length-1];

    	for( int i=A.length-2;i>=0;i--)
    	{
    		sum=0.0;
    		for(int j=i+1;j<A[0].length;j++)
    		{
    			sum=sum+A[i][j]*B[j];
    		}
    		B[i]=(B[i]-sum)/A[i][i];
    	}
    };
};
